﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.SessionState;
using System.Drawing;
using CRM.Controller;
using CRM.Entity;
using CRM.Entity.Extra;
using CRM.Common;


namespace CRM.Web.Admin.Ajax
{
    /// <summary>
    /// UserInfoAjax 职员的相关操作类
    /// </summary>
    public class UserInfoAjax : IHttpHandler, IRequiresSessionState
    {
        UserInfoController userInfoController = new UserInfoController();

        public void ProcessRequest(HttpContext context)
        {
            string action = string.Empty;

            if (context.Request["action"] != null)
            {
                action = context.Request["action"];
            }

            switch (action)
            {
                case "SingleFunc":
                    SingleFunc(context);
                    break;

                case "ListFunc":
                    ListFunc(context);
                    break;

                case "AddFunc":
                    AddFunc(context);
                    break;

                case "EditFunc":
                    EditFunc(context);
                    break;

                case "DeleteFunc":
                    DeleteFunc(context);
                    break;
                    
                case "FileUploadFunc":
                    FileUploadFunc(context);
                    break;
            }
        }

        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="context"></param>
        private void DeleteFunc(HttpContext context)
        {
            var id = Convert.ToInt32(context.Request.Form["UserId"]);

            var isSuccess = userInfoController.Delete(new CRM.Entity.UserInfo() { UserId = id });

            context.Response.Write(isSuccess);
        }

        /// <summary>
        /// 编辑操作
        /// </summary>
        /// <param name="context"></param>
        private void EditFunc(HttpContext context)
        {
            var single = context.Request.Form["json"].ParseJSON<CRM.Entity.UserInfo>();

            context.Response.Write(userInfoController.Update(single));
        }

        /// <summary>
        /// 添加操作
        /// </summary>
        /// <param name="context"></param>
        private void AddFunc(HttpContext context)
        {
            var single = context.Request.Form["json"].ParseJSON<CRM.Entity.UserInfo>();

            context.Response.Write(userInfoController.Add(single));
        }

        /// <summary>
        /// 获取集合
        /// </summary>
        /// <param name="context"></param>
        private void ListFunc(HttpContext context)
        {
            var pageIndex = Convert.ToInt32(context.Request.Form["pageIndex"]);

            var pageSize = Convert.ToInt32(context.Request.Form["pageSize"]);

            var pageInfo = new PageInfo()
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };

            var userInfoPage = userInfoController.GetListExtra(pageInfo);

            if (userInfoPage.ListEntity == null)
                userInfoPage.ListEntity = new List<CRM.Entity.Extra.UserInfoExtra>();


            context.Response.Write(JsonHelper.ListToJson(userInfoPage.ListEntity, userInfoPage.RecordCount.ToString()));
        }

        /// <summary>
        /// 获取单条
        /// </summary>
        /// <param name="context"></param>
        private void SingleFunc(HttpContext context)
        {
            var id = Convert.ToInt32(context.Request.Form["UserId"]);

            var single = userInfoController.GetSingle(id);

            context.Response.Write(single.ToJson());
        }
        
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="context"></param>
        private void FileUploadFunc(HttpContext context)
        {
            var file = context.Request.Files["Filedata"];

            var relationPath = "/Content/file/";

            var path = context.Server.MapPath(relationPath);

            var fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff") + ".jpg";

            var fullName = path + fileName;

            var relationFullName = relationPath + fileName;

            if (file != null)
            {
                DirectoryHelper.CreateDirectory(path);

                var image = Image.FromStream(file.InputStream);

                image.Save(fullName);
                image.Dispose();
            }

            context.Response.Write(relationFullName);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}